﻿(function (window) {
    //加载动画对象
    var loader = {};

    //绘制动画
    loader.draw = function (index, x, y) {
        index = index | 0;
        switch (index)
        {
            case 1: draw1(x, y); break;
            default:
                draw0(x, y);
                break;
        }
    };

    //绘制动画0
    function draw0(x, y) {
        var sb = spriteBatch;
        sb.setShadow(0, 0, 10, "#fff");
        sb.drawCircle(x, y, 20, "#111", 18);
        sb.setShadow(0, 0, 2, "#ddd");
        for (var i = 0; i < 15; i++)
        {
            if (draw0.angle[i] == 360) { draw0.angle[i] = 0; }
            var cx = 20 * Math.cos((draw0.angle[i]) * Math.PI / 180) + x;
            var cy = 20 * Math.sin((draw0.angle[i]) * Math.PI / 180) + y;
            sb.fillCircle(cx, cy, 5, "rgba(100,190,255," + draw0.alpha[i] + ")");
            draw0.angle[i] += 7;
            if (draw0.angle[i] > 367) draw0.angle[i] -= 360;
        }
        sb.setShadow();
    }
    draw0.angle = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150];
    draw0.alpha = [0.06, 0.12, 0.18, 0.26, 0.32, 0.39, 0.47, 0.51, 0.58, 0.64, 0.7, 0.78, 0.82, 0.9, 1];

    //绘制动画1
    function draw1(x, y) {
        if (!draw1.canvas)
        {
            var c = document.createElement("canvas");
            c.width = 400;
            c.height = 100;
            draw1.canvas = c;
            draw1.lightLoaderObj = new draw1.lightLoader(c, c.width, c.height);
        }
        var canvasCtx = graphics.getContext();
        graphics.setContext(draw1.canvas.getContext("2d"));

        draw1.lightLoaderObj.draw();
        graphics.setContext(canvasCtx);
        spriteBatch.draw(draw1.canvas, x, y);
    }
    draw1.lightLoader = function (c, cw, ch) {
        var _this = this;
        this.c = c;
        this.cw = cw;
        this.ch = ch;
        this.loaded = 0;
        this.loaderSpeed = .6;
        this.loaderHeight = 10;
        this.loaderWidth = 310;
        this.loader = {
            x: (this.cw / 2) - (this.loaderWidth / 2),
            y: (this.ch / 2) - (this.loaderHeight / 2)
        };
        this.particles = [];
        this.particleLift = 180;
        this.hueStart = 0
        this.hueEnd = 120;
        this.hue = 0;
        this.gravity = .15;
        this.particleRate = 4;

        this.rand = function (rMi, rMa) { return ~~((Math.random() * (rMa - rMi + 1)) + rMi); };
        this.hitTest = function (x1, y1, w1, h1, x2, y2, w2, h2) { return !(x1 + w1 < x2 || x2 + w2 < x1 || y1 + h1 < y2 || y2 + h2 < y1); };

        this.updateLoader = function () {
            if (this.loaded < 100)
            {
                this.loaded += this.loaderSpeed;
            } else
            {
                this.loaded = 0;
            }
        };

        this.renderLoader = function () {
            var sb = spriteBatch;

            sb.fillRect(this.loader.x, this.loader.y, this.loaderWidth, this.loaderHeight, "#000");

            this.hue = this.hueStart + (this.loaded / 100) * (this.hueEnd - this.hueStart);

            var newWidth = (this.loaded / 100) * this.loaderWidth;
            sb.fillRect(this.loader.x, this.loader.y, newWidth, this.loaderHeight, "hsla(" + this.hue + ", 100%, 40%, 1)");

            sb.fillRect(this.loader.x, this.loader.y, newWidth, this.loaderHeight / 2, "#222");
        };

        this.Particle = function () {
            this.x = _this.loader.x + ((_this.loaded / 100) * _this.loaderWidth) - _this.rand(0, 1);
            this.y = _this.ch / 2 + _this.rand(0, _this.loaderHeight) - _this.loaderHeight / 2;
            this.vx = (_this.rand(0, 4) - 2) / 100;
            this.vy = (_this.rand(0, _this.particleLift) - _this.particleLift * 2) / 100;
            this.width = _this.rand(1, 4) / 2;
            this.height = _this.rand(1, 4) / 2;
            this.hue = _this.hue;
        };

        this.Particle.prototype.update = function (i) {
            this.vx += (_this.rand(0, 6) - 3) / 100;
            this.vy += _this.gravity;
            this.x += this.vx;
            this.y += this.vy;

            if (this.y > _this.ch)
            {
                _this.particles.splice(i, 1);
            }
        };

        this.Particle.prototype.render = function () {
            spriteBatch.fillRect(this.x, this.y, this.width, this.height, "hsla(" + this.hue + ", 100%," + _this.rand(50, 70) + "%, " + _this.rand(20, 100) / 100 + ")");
        };

        this.createParticles = function () {
            var i = this.particleRate;
            while (i--)
            {
                this.particles.push(new this.Particle());
            };
        };

        this.updateParticles = function () {
            var i = this.particles.length;
            while (i--)
            {
                var p = this.particles[i];
                p.update(i);
            };
        };

        this.renderParticles = function () {
            var i = this.particles.length;
            while (i--)
            {
                var p = this.particles[i];
                p.render();
            };
        };

        this.clearCanvas = function () {
            var ctx = graphics.getContext();
            ctx.globalCompositeOperation = 'source-over';
            ctx.clearRect(0, 0, this.cw, this.ch);
            ctx.globalCompositeOperation = 'lighter';
        };

        this.draw = function () {
            _this.clearCanvas();
            _this.createParticles();
            _this.updateLoader();
            _this.updateParticles();
            _this.renderLoader();
            _this.renderParticles();
        };

    };
    draw1.canvas = null;

    //公开对象
    window.loader = loader;
})(window);